package me.zhenqu.web.util;

/**
 * Created by IntelliJ IDEA.
 * User: yibing.tan
 * Date: 11-11-16
 * Time: 下午5:33
 * To change this template use File | Settings | File Templates.
 */

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import me.zhenqu.model.pojo.entity.User;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

public class UserInterceptor extends HandlerInterceptorAdapter {
    private String prohibit;
    private String signinPage;

    public UserInterceptor(String prohibit,String signinPage){
        this.prohibit = prohibit;
        this.signinPage = signinPage;
    }

    public boolean preHandle(HttpServletRequest request,HttpServletResponse response,Object handler)throws Exception {
        final String signinTag = request.getContextPath()+signinPage;
        String  p = request.getRequestURI();
        if (!p.contains(prohibit))
            return true;
        try{
            User user = SessionUtil.loadUser(request.getSession());
            if (user==null){
                response.sendRedirect(signinTag);
                return false;
            }
        }catch (Exception e){
            response.sendRedirect(signinTag);
            return false;
        }
        return true;
    }
}
